<script setup>
import { RouterLink, RouterView, routerKey } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus';
import {useRouter} from 'vue-router';
import { ref, reactive, onMounted } from 'vue'
import { default as http, getImageUrl } from '@/utils/request.js';
const active = ref(2);
const router = useRouter();
const onClickLeft = () => router.push("/personal");

// Alt + Shift +F 整理代码格式
// 待收货列表
const afterSendList = reactive([

])

// 获取待收货列表  
const getAfterSendList = () => {
  // console.log("------------" + user.value);
  http.get("/user/info").then((response) => {
    console.log("获取个人信息= ", response);
    console.log("id:", response.data.data.userName);
    http.get("/user/order/aftersend/" + response.data.data.userName).then((res) => {
      afterSendList.splice(0); //todo 需要将原有的元素清空  
      // afterSendList = afterSendList.filter(res.data.data.list.orderNumber); 
      res.data.data.list.forEach(order => {
        afterSendList.push(order);
      });
      console.log('获取待收货列表: ', res.data.data.list);
      console.log('afterSendList:', afterSendList);
      // resolve(); // 当获取待收货列表成功后，返回一个解析成功的Promise  
    }).catch(err => {
      reject(err); // 当请求失败时，返回一个请求失败的Promise  
    });

  }).catch(() => {
  });
};

// 确认收货
const ackAfterSend = (orderNumber) => {
  console.log("================确认收货")
  // 二次确认
  ElMessageBox.confirm(`确定要收货吗？`, '提示', {
    type: 'warning'
  }).then(() => {//用户点击确定按钮
      http.put("/user/order/orderstatus/"+orderNumber)
        .then((res) => {
          console.log('返回值：', res);
          if (res.data.code === 200) {	//修改成功
            ElMessage.success(`收货成功`);
            getAfterSendList();
          }
        }).catch((err) => {
          console.log('收货失败：', err);
          ElMessage.error(`失败`);
        });
    })
    .catch(() => {//点击取消按钮

    });

}

// 打开订单
// '/orderinfo/:orderNumber(\\d+)',
// router.push({path: '/suborder/' +orderNumber})

const toOrder = function (orderNumber) {
    console.log("订单号:", orderNumber);
    router.push({path: '/orderinf/' + orderNumber});
    // router.push({name: 'orderinf', params: { orderNumber }})
}

onMounted(() => {
  // getparseJWT();
  getAfterSendList();
  // handleDelete(); 
  // 获取个人信息  

});



</script>


<template>
          <van-col span="24" class="nav_bar">
            <van-nav-bar title="待发货" left-text="返回" :fixed="true" left-arrow @click-left="onClickLeft" />
          </van-col>
       
      <van-cell-group>
        <van-tabs v-model:active="active" style="margin-top: 25px;">
          <van-tab title="待付款" to="/beforepay">待付款</van-tab>
          <van-tab title="待发货" to="/backorder"></van-tab>
          <van-tab title="待收货" ></van-tab>
          <van-tab title="售 后" to="/returnorder"></van-tab>
        </van-tabs>
      </van-cell-group>
  <div style="color: #c8d175; margin-top: 10px;">
    <!-- <van-search v-model="value" shape="round" background="rgb(218, 218, 218)" placeholder="请输入搜索关键词" /><br> -->
    <van-cell-group inset >  
        <van-card v-for="as in afterSendList" :key="as.orderNumber" :num="as.amount" :price="as.orderPrice"
          :desc="as.details" :title="as.sellName" :thumb="getImageUrl(as.picture)"
          style="margin-top: 10px; background-color:  #FFFFE0;" @click="toOrder(as.orderNumber)">
          <template #tags>
            <van-tag plain type="primary" style="flex-grow: 1; margin-right: 10px;">{{ as.goodsName }}</van-tag>
          </template>
          <template #footer style="margin-left: 0%;">
            <van-space size="1rem">
              <van-button size="mini" @click.stop="ackAfterSend(as.orderNumber)">确认收货</van-button>
            </van-space>
          </template>
        </van-card>
    </van-cell-group>
  </div>

  <RouterView />
</template>

<style scoped>
header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 9999;
  line-height: 1.5;
  max-height: 100vh;
}


header {
  line-height: 1.5;
  max-height: 100vh;
}


nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }


  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
    padding: 1rem 0;
    margin-top: 1rem;
  }
}

.van-field {
  /* 调整每一行边距 */
  padding: 16px 15px;
}

.van-nav-bar {
  color: #d4e09f;
  width: 100%;
}

.full-width-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 10px;
  /* 根据需要调整 */
}
.nav_bar{
    position:relative;
    top:-32px;
    /* left:-32px;
    right: -32px; */
    margin:2px -32px
}
</style>
